package com.nodecollege.cloud.service.impl.deal;

import com.alibaba.fastjson.JSON;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.po.ChatGroupUser;
import com.nodecollege.cloud.common.model.vo.AddGroupVO;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.ChatService;
import com.nodecollege.cloud.service.GroupUserService;
import com.nodecollege.cloud.service.impl.ChatServiceAbstrat;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 添加群用户信息
 *
 * @author LC
 * @date 2020/2/26 23:07
 */
@Slf4j
@Service
public class ChatAddGroupUserServiceImpl extends ChatServiceAbstrat implements ChatService {

    @Autowired
    private GroupUserService groupUserService;

    /**
     * 获取群组用户信息
     *
     * @param chatData
     * @param channel
     */
    @Override
    public void handler(String chatData, Channel channel) {
        log.info("ChatAddGroupUserServiceImpl handler");
        AddGroupVO groupData = JSON.parseObject(chatData, AddGroupVO.class);
        Long userId = ThreadUserUtils.getLoginUser().getUserId();
        AddGroupVO groupUser = new AddGroupVO();
        groupUser.setUserId(userId);
        groupUser.setGroupId(groupData.getGroupId());
        groupUser.setUserList(groupData.getUserList());
        List<ChatGroupUser> userList = groupUserService.addGroupUser(groupUser);
        // 给群所有人发送发送
        if (!userList.isEmpty()) {
            sendUpdateGroupUser(userList);
        }
    }

    /**
     * 向群组所有人发送群用户更新数据
     *
     * @param addUserList
     */
    public void sendUpdateGroupUser(List<ChatGroupUser> addUserList) {
        sendGroupMsg(addUserList.get(0).getGroupId(), NCResult.ok(addUserList), 8);
    }
}
